﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;

namespace AlfarEngine.Utilities.ShadowMapping
{
    class ShadowmapResolverSpot : ShadowmapResolver
    {
        public Components.SpotLight Spot;


        /// <summary>
        /// Creates a new shadowmap resolver
        /// </summary>
        /// <param name="graphicsDevice">The Graphics Device used by the XNA game</param>
        /// <param name="quadRender"></param>
        /// <param name="baseSize">The size of the light regions </param>
        public ShadowmapResolverSpot(GraphicsDevice graphicsDevice, QuadRenderComponent quadRender, ShadowmapSize maxShadowmapSize, ShadowmapSize maxDepthBufferSize)
        :base(graphicsDevice, quadRender, maxShadowmapSize, maxDepthBufferSize)
        {
            
        }

        public override void LoadContent()
        {
            base.LoadContent();

            resolveShadowsEffect = Engine.Content.Load<Effect>("effects/resolveShadowsEffect_Spot");
        }

        protected override void ExecuteTechnique(Texture2D source, RenderTarget2D destination, string techniqueName, Texture2D shadowMap)
        {
            Vector2 renderTargetSize;
            renderTargetSize = new Vector2((float)baseSize, (float)baseSize);
            graphicsDevice.SetRenderTarget(destination);
            graphicsDevice.Clear(Color.White);
            resolveShadowsEffect.Parameters["renderTargetSize"].SetValue(renderTargetSize);

            if (source != null)
                resolveShadowsEffect.Parameters["InputTexture"].SetValue(source);
            if (shadowMap != null)
            {
                resolveShadowsEffect.Parameters["ShadowMapTexture"].SetValue(shadowMap);
                resolveShadowsEffect.Parameters["Direction"].SetValue(Spot.direction);
                resolveShadowsEffect.Parameters["Angle"].SetValue(MathHelper.ToRadians(Spot.angle));
            }

            resolveShadowsEffect.CurrentTechnique = resolveShadowsEffect.Techniques[techniqueName];

            foreach (EffectPass pass in resolveShadowsEffect.CurrentTechnique.Passes)
            {
                pass.Apply();
                quadRender.Render(Vector2.One * -1, Vector2.One);
            }
            graphicsDevice.SetRenderTarget(null);
        }

    }
}
